#include "stringFuncs.h"
#include <iostream>

void stringFuncs::Str_split(const std::string& src, const std::string& separator , std::vector<std::string>& dest)
{
	std::string str = src.c_str();
	std::cout<<str.c_str()<<std::endl;
	int index;
	int seplen = strlen(separator.c_str());
	do 
	{
		index = str.find(separator);
		if (index < 0)
		{
			break;
		}
		std::string substr = str.substr(0, index);
		dest.push_back(substr);
		str = str.substr(index + seplen);
	} while (index > -1);
	dest.push_back(str);
}

void stringFuncs::Str_join(const std::vector<std::string>& src, const std::string& separator, std::string &dest)
{
	dest = "";
	int count = src.size();
	for(int i = 0; i != count; i++)
	{
		if(i != 0)
		{
			dest += separator;
		}
		dest += src[i];
	}
}

bool stringFuncs::Char_match(const char *src, const char * target)
{
	int srclen = strlen(src);
	int tarlen = strlen(target);

	if(srclen != tarlen)
	{
		return false;
	}
	int i = 0;
	for(; i < srclen; i++)
	{
		if(src[i] != target[i])
			return false;
	}
	if(i == srclen)
		return true;

	return false;
}

bool stringFuncs::Char_macthBegin(const char *src, const char * target)
{
	int srclen = strlen(src);
	int tarlen = strlen(target);

	if(srclen < tarlen)
		return false;
	int i = 0;
	for(; i < tarlen; i++ )
	{
		if(src[i] != target[i])
		{
			return false;
		}
	}
	return true;
}

void stringFuncs::rand_str(char * str, const int len)
{    
	int i;
	for(i=0; i < len; i++ )
	{
		str[i] = 'a' + rand()%26;
	}
	str[i] = '\0';
}

void stringFuncs::trim(std::string &src,std::string &dst)
{
	int i = 0;
	while(src[i] == ' ' || src[i] == '\t' || src[i] == '\n')
	{
		i++;
	}
	dst = src.substr(i);
	i = dst.size()-1;
	while(src[i] == ' ' || src[i] == '\t' || src[i] == '\n')
	{
		i--;
	}
	dst = dst.substr(0,i+1);
}

void stringFuncs::replace(std::string& str,const std::string& old_value, const std::string& new_value)
{
	for(std::string::size_type pos(0); pos!=std::string::npos; pos+=new_value.length())  
	{     
		if((pos=str.find(old_value,pos)) != std::string::npos)     
		{ 
			str.replace(pos,old_value.length(),new_value);  
		}   
		else  { break; }    
	}  
}
